package com.focusai.efairy.ui.adapter.dev;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.dev.DeviceItem;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.utils.StrByCodeUtils;

/**
 * Created by luo_guangwen on 2017/7/31.
 */

public class DevLocationAdapter extends BaseRecyclerAdapter<DeviceItem> {

    private int selectPostion = -1;
    private OnItemViewClickListener onItemViewClickListener;

    public DevLocationAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_project_location;
    }

    @Override
    protected void onBindBaseRecyclerViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        final DeviceItem item = mList.get(position);
        holder.setText(R.id.tv_project_name, item.efairydevice_name);
        holder.setText(R.id.tv_project_adress, item.efairydevice_address);
        if (position == selectPostion) {
            holder.getView(R.id.llayout_item).setBackgroundColor(mContext.getResources().getColor(R.color.bg_gray));
        } else {
            holder.getView(R.id.llayout_item).setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }
        holder.getView(R.id.llayout_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPostion = position;
                if (onItemViewClickListener != null) {
                    onItemViewClickListener.onItemClick(holder, position);
                    notifyDataSetChanged();
                }
            }
        });
        holder.getView(R.id.ll_go_here).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPostion = position;
                if (onItemViewClickListener != null) {
                    onItemViewClickListener.onItemClickGoHere(item, position);
                }
            }
        });
        int drawableId = R.drawable.icon_address_red;
        if (StrByCodeUtils.getColorByDevState(item.efairydevice_alarm_id) == R.color.dev_status_yellow){
            drawableId = R.drawable.icon_address_yellow;
        }else if (StrByCodeUtils.getColorByDevState(item.efairydevice_alarm_id) == R.color.dev_status_green){
            drawableId = R.drawable.icon_address_green;
        }
        ((ImageView)holder.getView(R.id.iv_location)).setImageResource(drawableId);
    }

    public void setOnItemViewClickListener(OnItemViewClickListener onItemViewClickListener) {
        this.onItemViewClickListener = onItemViewClickListener;
    }

    /**
     * item点击事件回调接口
     */
    public interface OnItemViewClickListener {
        void onItemClick(BaseRecyclerViewHolder holder, int position);
        void onItemClickGoHere(DeviceItem item, int position);
    }

}
